package org.pezke.akunature.dao.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.pezke.akunature.dao.FBachDao;
import org.pezke.akunature.model.fb.FBach;
import org.pezke.akunature.model.fb.FBachCriteria;
import org.springframework.stereotype.Repository;


/**
 * @author igarcia
 */
@Repository("fbDao")
public class FBachDaoHibernate extends GenericDaoHibernate<FBach, Long> implements FBachDao {

	
	/**
     * Constructor to create a Generics-based version using Role as the entity
     */
    public FBachDaoHibernate() {
        super(FBach.class);
    }
		
	
	/**
	 * Devuelve la lista de elementos FB a partir de los criterios seleccionados
	 */
	@SuppressWarnings("unchecked")
	public List<FBach> search(FBachCriteria criteria) {
		
		Criteria dc = getSession().createCriteria(FBach.class);
		dc.setProjection(Projections.distinct(Projections.property("id")));
		
		if(criteria!=null){
			if (criteria.getName() != null) {
				dc = dc.add(Restrictions.ilike("name", "%" + criteria.getName() + "%"));
			}

			if (criteria.getDescription() != null) {
				dc = dc.add(Restrictions.ilike("description", "%" + criteria.getDescription() + "%"));
			}

			if (criteria.getGroup() != null) {
				dc = dc.add(Restrictions.ilike("group.name", "%" + criteria.getGroup() + "%"));
			}
			
			if (criteria.getGroupId() != null) {
				dc = dc.add(Restrictions.eq("group.id", criteria.getGroupId()));
			}

			if (criteria.getNameEng() != null) {
				dc = dc.add(Restrictions.ilike("nameEng", "%" + criteria.getNameEng() + "%"));
			}

			if (criteria.getNameLat() != null) {
				dc = dc.add(Restrictions.ilike("nameLat", "%" + criteria.getNameLat() + "%"));
			}
			
			if (criteria.getSample() != null) {
				dc = dc.add(Restrictions.ilike("sample", "%" + criteria.getSample() + "%"));
			}
			
			if (criteria.getKeys() != null) {
				dc = dc.add(Restrictions.ilike("keys.name", 
						"%" + criteria.getKeys() + "%"));
			}
			
			if (criteria.getConsiderations() != null) {
				dc = dc.add(Restrictions.ilike("considerations.name", 
						"%" + criteria.getConsiderations() + "%"));
			}
		}
		
		
		List<FBach> result = dc.list();
		return result;
	}


}
